package pl.klangner.recobox
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.UIComponent;
	
	import pl.klangner.recobox.classifier.DirectionClassifier;
	import pl.klangner.recobox.classifier.InkMultiStroke;

	public class CharBox extends UIComponent
	{
		[Bindable] public var character:String;
		[Bindable] public var debugInfo:String;
		
	  // --------------------------------------------------------------------------------------------
		public function CharBox() 
		{
		  _classifier = new DirectionClassifier();
		  clear();
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); 
    }
        
	  // --------------------------------------------------------------------------------------------
	  public function get classifier():DirectionClassifier{
	  	return _classifier;
	  }
	  
	  // --------------------------------------------------------------------------------------------
	  public function get stroke():InkMultiStroke{
	  	return _stroke;
	  }
	  
	  // --------------------------------------------------------------------------------------------
	  public function clear():void{
    	while(numChildren > 0){
   			removeChildAt(0);
 			}
 			
 			_stroke = new InkMultiStroke();
    	_sprite = new Sprite();
    	_sprite.graphics.lineStyle(_thickness, _color);
    	this.addChild(_sprite);
	  }
	  
	  // --------------------------------------------------------------------------------------------
    private function onMouseDown(e:MouseEvent):void
    {
    	_sprite.graphics.moveTo(mouseX, mouseY);
    	_stroke.addStroke();
    	_stroke.addPoint(new Point(mouseX,mouseY));
    	
		  character = "";
		  debugInfo = "";
    	
    	this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    	this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 
    } 

	  // --------------------------------------------------------------------------------------------
    private function onMouseMove(e:MouseEvent):void 
    {
    	_sprite.graphics.lineTo(mouseX, mouseY);
    	_stroke.addPoint(new Point(mouseX,mouseY));
    }
        
	  // --------------------------------------------------------------------------------------------
    private function onMouseUp(e:MouseEvent):void 
    {
    	this.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

    	_stroke.addPoint(new Point(mouseX,mouseY));
    	drawStroke(_stroke);
		  character = _classifier.classify(_stroke);
		  debugInfo = _classifier.gestures;
    }
        
	  // --------------------------------------------------------------------------------------------
    override protected function updateDisplayList(w:Number, h:Number):void
    {
    	var g:Graphics = this.graphics;
    	
    	g.clear();
    	g.beginFill(_bgColor, 1);
      g.drawRect(0, 0, unscaledWidth, unscaledHeight);
      g.endFill();
        
    }
        
	  // --------------------------------------------------------------------------------------------
    private function drawStroke(stroke: InkMultiStroke):void
    {
    	var sprite: Sprite = new Sprite();
    	sprite.graphics.lineStyle(1, 0xFF0000);

      stroke.drawSimplyfied(sprite.graphics);
    	this.addChild(sprite);
    }
    
    
    
		// --------------------------------------------------------------------------------------------
		// Private members
		private var _classifier: DirectionClassifier;
		private var _sprite : Sprite; // Aktualnie rysyowany obraz. Znika po zakonczeniu rysowania.
		private var _stroke : InkMultiStroke;  // zapamietywane kolejne punkty rysowanego obrazu.
		private var _bgColor : int = 0xFFFFFF;
		private var _color : Number;
		private var _thickness : Number = 3;

	}
}
